using System.Configuration;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using RecruitmentAgency.Models.DataProviders;
using System.Collections.Generic;


namespace RecruitmentAgency.Controllers
{
    /// <summary>
    /// The RecruitmentAgency.Controllers.BaseController provides methods and properties useful to all Controllers in the RecruitmentAgency web site
    /// </summary>
    public class BaseController : Controller
    {
        private int currentPersonId = -1;
        private string role = string.Empty;
        private const int defaultPageSize = 20;
        private const int defaultMinimumPasswordLength = 8;
        private List<string> defaultPageSizes;

        /// <summary>
        /// Initialises a new RecruitmentAgency.Controllers.BaseController instance
        /// </summary>
        public BaseController()
        {
            ViewData["role"] = Role;
        }

        /// <summary>
        /// Gets the ICvDataProvider instance that provides access to the CV database tables
        /// </summary>
        protected ICvDataProvider DataProvider
        {
            get
            {
                return new CvDataProvider();
            }
        }

        /// <summary>
        /// Gets the IJobProviderDataProvider instance that provides access to the job provider database tables
        /// </summary>
        protected IJobProviderDataProvider JobProviderDataProvider
        {
            get
            {
                return new JobProviderDataProvider();
            }
        }

        /// <summary>
        /// Gets the IAccountDataProvider instance that provides access to the user account database tables
        /// </summary>
        protected IAccountDataProvider AccountDataProvider
        {
            get
            {
                return new AccountDataProvider();
            }
        }

        /// <summary>
        /// Gets or sets the ID number of the currently logged in user
        /// </summary>
        public int CurrentPersonId
        {
            get
            {
                if (currentPersonId == -1 && UserIsAuthenticated)
                {
                    currentPersonId = DataProvider.GetPersonIdByUsername(System.Web.HttpContext.Current.User.Identity.Name);
                }
                return currentPersonId;
            }
            set
            {
                currentPersonId = value;
            }
        }

        /// <summary>
        /// Gets or sets the role of the user
        /// </summary>
        public string Role
        {
            get
            {
                if (role == string.Empty)
                {
                    if (UserIsAuthenticated)
                    {
                        switch ((((FormsIdentity)System.Web.HttpContext.Current.User.Identity).Ticket.UserData).Split(',')[1])
                        {
                            case "1": role = "Admin"; break;
                            case "3": role = "JobSeeker"; break;
                            case "4": role = "JobProvider"; break;
                            default: role = "None"; break;
                        }
                    }
                    else role = "None";
                }
                return role;
            }
            set
            {
                role = value;
            }
        }

        /// <summary>
        /// Gets a Boolean value that determines whether the current user is authenticated (logged in)
        /// </summary>
        protected bool UserIsAuthenticated
        {
            get
            {
                return System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
            }
        }

        /// <summary>
        /// Gets a Boolean value that determines whether the currently logged in user is in the Admin role
        /// </summary>
        protected bool IsAdmin
        {
            get
            {
                if (UserIsAuthenticated)
                {
                    return ((((FormsIdentity) System.Web.HttpContext.Current.User.Identity).Ticket.UserData).Split(',')[1] == "1");                        
                }
                return false;
            }
        }

        /// <summary>
        /// Gets a Boolean value that determines whether the currently logged in user is in the jobProvider role
        /// </summary>
        public bool IsJobProvider
        {
            get
            {
                if (UserIsAuthenticated)
                {
                    return ((((FormsIdentity)System.Web.HttpContext.Current.User.Identity).Ticket.UserData).Split(',')[1] == "4");
                }
                return false;
            }
        }

        /// <summary>
        /// Gets the page size from the web.config file, or if that is missing gets the default page size. This value can also be set and refers to the number of records displayed in the search result pages
        /// </summary>
        protected int PageSize
        {
            get
            {
                if (HttpRuntime.Cache["pageSize"] != null) return (int)HttpRuntime.Cache["pageSize"];
                else
                {
                    int pageSize = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["PageSize"]) ? int.Parse(ConfigurationManager.AppSettings["PageSize"]) : defaultPageSize;
                    HttpRuntime.Cache["pageSize"] = pageSize;
                    return pageSize;
                }
            }
            set
            {
                HttpRuntime.Cache["pageSize"] = value;
            }
        }

        /// <summary>
        /// Gets the minimum allowable length for a password
        /// </summary>
        protected int MinimumPasswordLength
        {
            get
            {
                if (HttpRuntime.Cache["minimumPasswordLength"] != null) return (int)HttpRuntime.Cache["minimumPasswordLength"];
                else
                {
                    int minimumPasswordLength = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["MinimumPasswordLength"]) ? int.Parse(ConfigurationManager.AppSettings["MinimumPasswordLength"]) : defaultMinimumPasswordLength;
                    HttpRuntime.Cache["minimumPasswordLength"] = minimumPasswordLength;
                    return minimumPasswordLength;
                }
            }
        }

        /// <summary>
        /// Gets the default list of page sizes of search results
        /// </summary>
        protected List<string> DefaultPageSizes
        {
            get
            {
                defaultPageSizes = new List<string> {"10", "20", "30", "40", "50", "100"};
                return defaultPageSizes;
            }
        }

        /// <summary>
        /// Clears the page size that is stored in the Cache
        /// </summary>
        protected void ResetPageSize()
        {
            HttpRuntime.Cache.Remove("pageSize");
        }
    }
}